package org.tote.service.user;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.tote.command.UserCommandLight;
import org.tote.exception.UserAlreadyExistException;
import org.tote.model.User;
import org.tote.model.UserLight;

public interface UserService {

	/**
	 * Saves or updates user entity
	 * 
	 * @param user
	 *            - user for saving or updating
	 */
	public void saveOrUpdateUser(User user);

	/**
	 * Produces registration for user
	 * 
	 * @param user
	 *            - user for registration
	 * @throws UserAlreadyExistException
	 *             if user with specified login doesn't exist.
	 */
	public void registerUser(User user) throws UserAlreadyExistException;

	public void adminCreateUser(User user) throws UserAlreadyExistException;

	/**
	 * Generates new password for user with specified login and notifies him via email.
	 * 
	 * @param login
	 *            login of user to generate new password for.
	 * @throws UserAlreadyExistException
	 *             if user with specified login doesn't exist.
	 */
	public void restorePassword(String login) throws UserAlreadyExistException;

	/**
	 * Updates password of user by conversion to MD5 hash
	 * 
	 * @param user
	 *            - user who's password needs to convert
	 */
	public void hashPassword(User user);

	/**
	 * Gets user by it's user details ID
	 * 
	 * @param id
	 *            - id of user's UserDetails
	 * @return user - user with specified UserDetails ID
	 */
	public User getUserById(Long id);

	/**
	 * Checks if user with specified login registered within the system.
	 * 
	 * @param login
	 *            - login to check for availability.
	 * @return true if name is available, false - otherwise.
	 */
	public boolean userExits(String login);

	/**
	 * Removes user from database by it's UserDetails ID
	 * 
	 * @param id
	 *            - ID of user's UserDetils
	 */
	public void removeUser(Long id);

	/**
	 * Gets all users from the database
	 * 
	 * @return List of users
	 */
	public List<User> getAllUsers();

	/**
	 * 
	 * @param page
	 *            - Number of page (starting from 0)
	 * @param limit
	 *            - number of users per page
	 * @return (limit+1) of users (or less). Additional user is given to know if the next page exists.
	 */
	public List<User> getFilteredUsersPage(int page, int limit, String usersFilter);

	/**
	 * Remove list of users from database by their ID's
	 * 
	 * @param idList
	 *            - list of ID's of users to remove
	 */
	public void removeUserList(List<String> nameList);

	/**
	 * Remove list of users from database by their ID's
	 * 
	 * @param idList
	 *            - list of ID's of users to remove
	 */
	public List<User> getUserListExcluding(List<String> nameList);

	/**
	 * Remove list of users from database by their ID's
	 * 
	 * @param nameList
	 *            - list of ID's of users to remove
	 * @param page
	 *            - number of page to return
	 * @param limit
	 *            - limit of the users to return
	 * @param usersFilter
	 *            - value to filter users with
	 */
	public List<User> getFilteredUsersExcludingPage(List<Long> excludeList, int page, int limit,
			String usersFilter);

	/**
	 * Returns list of users from database by their ID's
	 * 
	 * @param idList
	 *            - list of ID's of users to remove
	 */
	public List<User> getUserList(List<String> nameList);

	/**
	 * Returns current authorized user by it's username
	 * 
	 * @return - current user
	 */
	public User getCurrentAuthorizedUser();

	/**
	 * Authorizes new registered user by it's unique token, after successful authentication detaches token from user
	 * 
	 * @param token
	 *            - unique token
	 * @param request
	 *            - HTTP request for registration user in session
	 * @return - true, if token exists to user
	 */
	public boolean authorizeUserByToken(String token, HttpServletRequest request);

	/**
	 * Disables user, looking for him by it's id
	 * 
	 * @param id
	 *            - user's details id
	 * @return - true, if user's state changed
	 */
	public boolean disableUserById(Long id);

	/**
	 * Enables user, looking for him by it's id
	 * 
	 * @param id
	 *            - user's details id
	 * @return - true, if user's state changed
	 */
	public boolean enableUserById(Long id);

	/**
	 * 
	 * @param id
	 *            - user's details id
	 * @param enabled
	 *            - enable or disable user (set null to left unchanged)
	 * @param name
	 *            - change name of user (set null to left unchanged)
	 * @param password
	 *            - change user's password (set null to left unchanged)
	 */

	public void modifyUserModyfiable(Long id, Boolean enabled, String name, String password);

	/**
	 * Get user count.
	 * 
	 * @param limit
	 *            - limit of users per page
	 * @param usersFilter
	 *            - value to filter users
	 * @return - max users page
	 */
	public int getFilteredUsersMaxPage(int limit, String usersFilter);

	/**
	 * Get user count.
	 * 
	 * @param nameList
	 *            - list of users' names.
	 * @param limit
	 *            - limit of users per page
	 * @param ueersFilter
	 *            - value to filter users
	 * @return - max users page
	 */
	public int getFilteredUsersExcludingCount(List<Long> excludeList, int limit, String usersFilter);

	/**
	 * Logout all users from session *
	 * 
	 */
	public void logoutAllUsers();

	/**
	 * Logout user from session by it's User ID
	 * 
	 * @param id
	 *            - ID of user
	 */
	public void logoutUser(Long id);

	/**
	 * Returns list of users witch are logged
	 * 
	 */
	public List<UserCommandLight> getLoggedUsers();

	public List<String> getCurrentUserAutorities();

	public boolean isUserAdmin();

	User getUserByOpenId(String id);

	/**
	 * Convert authorities of current user to string.
	 * @return - current user authorities as String.
	 */
	String getCurrentUserAuthoritiesAsString();

	UserLight getUserLightById(Long id);

	User getUser(UserLight user);

	UserLight getCurrentAuthorizedUserLight();

	Long getCurrentAuthorizedUserId();

	UserLight getMaintenanceUserLight();
}
